﻿@using System.Globalization
@using Grand.Business.Core.Extensions
@using Grand.Business.Core.Interfaces.Common.Directory
@inject ICurrencyService currencyService
@inject IWorkContextAccessor workContextAccessor

@{
    var availableCurrencies = (await currencyService.GetAllCurrencies(storeId: workContextAccessor.WorkContext.CurrentStore.Id))
        .Select(x =>
        {
            //currency char
            var currencySymbol = !string.IsNullOrEmpty(x.DisplayLocale) ? new RegionInfo(x.DisplayLocale).CurrencySymbol : x.CurrencyCode;

            //model
            var currencyModel = new CurrencyModel
            {
                Id = x.Id,
                        Name = x.GetTranslation(y => y.Name, workContextAccessor.WorkContext.WorkingLanguage.Id),
                CurrencyCode = x.CurrencyCode,
                CurrencySymbol = currencySymbol
            };
            return currencyModel;
        }).ToList();

    if (availableCurrencies.Count > 1)
    {
        var returnUrl = Context?.Request?.GetEncodedPathAndQuery();
        var currencies = availableCurrencies.Select(x => new
        {
            x.Name,
            Text = x.CurrencyCode,
            Value = Url.RouteUrl("ChangeCurrency", new { currencyCode = x.CurrencyCode, returnUrl }),
            Selected = x.Id.Equals(workContextAccessor.WorkContext.WorkingCurrency.Id)
        });
        <b-dropdown class="mr-2" text="@currencies.FirstOrDefault(x => x.Selected).Text">
            @foreach (var item in currencies)
            {
                var className = item.Selected ? "active" : "";
                <b-dropdown-item class="@className" href="@item.Value">@item.Text</b-dropdown-item>
            }
        </b-dropdown>
    }
}